import dayjs from 'dayjs';

/**
 * js工具类
 */
export class JsUtil {
    /**
     * 获取文件后缀
     * @param {string} path 路径/文件名
     * @returns {any}
     */
    public static getType(path: string) {
        return path.substring(path.lastIndexOf("."), path.length);
    }

    /**
     * 根据唯一值field判断数据1中和数组2的内容差异性
     * @param arr1 数组1
     * @param arr2 数组2
     * @param fields 用于判断唯一的字段名称数组
     * @returns {{
     *              meet: Array, 数组1和数组2中都存在
     *              noMeet: Array, 只有数组1中存在
     *              noMeet2:Array  只有数组2中存在
     *           }}
     */
    public static splitArrayMeet(arr1: any, arr2: any, fields: string[]) {
        const meet = [];
        const noMeet = [];
        let isMeet;
        for (const item of arr1) {
            isMeet = false;
            for (const v of arr2) {
                let isMeet2 = true;
                for (const field of fields) {
                    if (item[field] !== v[field]) {
                        isMeet2 = false;
                        break;
                    }
                }
                if (isMeet2) {
                    isMeet = true;
                    break;
                }
            }
            if (isMeet) {
                meet.push(item);
            } else {
                noMeet.push(item);
            }
        }
        const noMeet2 = [];
        for (const item of arr2) {
            isMeet = false;
            for (const v of meet) {
                let isMeet2 = true;
                for (const field of fields) {
                    if (item[field] !== v[field]) {
                        isMeet2 = false;
                        break;
                    }
                }
                if (isMeet2) {
                    isMeet = true;
                    break;
                }
            }
            if (!isMeet) {
                noMeet2.push(item);
            }
        }
        return {meet, noMeet, noMeet2};
    }

    /**
     * 过滤掉数组中的相同数据
     * @param {any[]} arr 原始数组
     * @param {string[]} fields 唯一标识的的字段数组
     * @returns {any[]}
     */
    public static nodupArray(arr: any[], fields: string[]): any[] {
        const cacheObj: any = {};
        return arr.filter(item => {
            let uuid = "";
            for (const field of fields) {
                uuid += item[field];
            }
            if (cacheObj[uuid] === true) {
                return false
            } else {
                cacheObj[uuid] = true;
                return true
            }
        })
    }

    public static duplicate(arr: any[]) {
        let res: any = {}
        arr.forEach(item => {
            item.sort((a: any, b: any) => a - b);
            res[item] = item;
        });
        return Object.values(res)
    }

    public static tileCompletion(arr: any[]):any {
        const arr1 = JsUtil.duplicate(arr);
        const rowMapCol: any = {};
        arr1.forEach((rowCol: any) => {
            const [row, col] = rowCol;
            if (!rowMapCol[row]) {
                rowMapCol[row] = {max: col, min: col};
            }
            const {max, min} = rowMapCol[row];
            if (col > max) {
                rowMapCol[row].max = col;
            } else if (col <= min) {
                rowMapCol[row].min = col;
            }
        });
        const arr2 = [];
        for(let row of Object.keys(rowMapCol)){
            const colMinMax =  rowMapCol[row];
            for(let col = colMinMax.min;col<=colMinMax.max;col++){
                arr2.push([row,col]);
            }
        }
        return arr2;
    }
}
